package yw.nmh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import yw.nmh.mapper.PenInfoMapper;
import yw.nmh.mapper.RanchInfoMapper;
import yw.nmh.penInfo.domain.Pen;
import yw.nmh.penInfo.dto.PenDTO;
import yw.nmh.penInfo.dto.PenListDTO;
import yw.nmh.penInfo.dto.PenUpdateDTO;
import yw.nmh.penInfo.vo.PenVO;
import yw.nmh.ranchInfo.domain.Ranch;
import yw.nmh.result.PageDTO;
import yw.nmh.service.PenService;
import yw.nmh.utils.BeanConverter;

import java.util.Date;
import java.util.List;

@Service
public class PenServiceImpl extends ServiceImpl<PenInfoMapper, Pen> implements PenService {
    @Resource
    private PenInfoMapper penInfoMapper;

    @Resource
    private RanchInfoMapper ranchInfoMapper;
    @Override
    public Boolean addPen(PenDTO penDTO) {
        LambdaQueryWrapper<Pen> penLambdaQueryWrapper = new LambdaQueryWrapper<>();
        penLambdaQueryWrapper.eq(Pen::getPenName,penDTO.getPenName());
        Pen one = this.getOne(penLambdaQueryWrapper);
        if (one == null) {
            Pen pen = new Pen();
            pen.setPenName(penDTO.getPenName());
            pen.setIsEnable(1);
            pen.setFarmId(penInfoMapper.getRanchId(penDTO.getRanchName()));
            pen.setAmountLivestock(0);
            pen.setCreateTime(new Date());
            pen.setUpdateTime(new Date());
            penInfoMapper.insert(pen);
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateStatus(Integer penId) {
        Pen pen = penInfoMapper.selectById(penId);
        if (pen.getIsEnable() == 1){
            pen.setIsEnable(0);
            penInfoMapper.updateById(pen);
            return true;
        }
        if (pen.getIsEnable() == 0){
            pen.setIsEnable(1);
            penInfoMapper.updateById(pen);
            return true;
        }
        return false;
    }

    //修改圈舍
    @Override
    public Boolean updatePen(PenUpdateDTO penUpdateDTO) {
        Pen pen = penInfoMapper.selectById(penUpdateDTO.getPenId());
        if (penInfoMapper.getRanchId(penUpdateDTO.getRanchName()) == null){
            throw new RuntimeException("牧场不存在");
        }
        LambdaQueryWrapper<Pen> penLambdaQueryWrapper = new LambdaQueryWrapper<>();
        penLambdaQueryWrapper.eq(Pen::getPenName,penUpdateDTO.getPenName())
                .eq(Pen::getPenId,penInfoMapper.getRanchId(penUpdateDTO.getRanchName()));
        Pen one = this.getOne(penLambdaQueryWrapper);
        if (one == null) {
            pen.setPenName(penUpdateDTO.getPenName());
            pen.setFarmId(penInfoMapper.getRanchId(penUpdateDTO.getRanchName()));
            pen.setUpdateTime(new Date());
            penInfoMapper.updateById(pen);
            return true;
        }

        return false;
    }

    //查询圈舍
    @Override
    public Page<PenVO> listPen(PenListDTO penListDTO) {
        PageDTO pageDTO = new PageDTO();
        BeanUtils.copyProperties(penListDTO.getPageDTO(),pageDTO);
        Page<Pen> penPage = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());
        LambdaQueryWrapper<Pen> penLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Integer> ranchIds = penInfoMapper.getRanchIds(penListDTO.getRanchName());
        penLambdaQueryWrapper.eq(Pen::getIsEnable,penListDTO.getIsEnable())
                .like(Pen::getPenName,penListDTO.getPenName())
                .in(Pen::getFarmId,ranchIds.toArray());

        Page<Pen> page = this.page(penPage, penLambdaQueryWrapper);
        Page<PenVO> penVOPage = new Page<PenVO>();
        penVOPage.setRecords(BeanConverter.convertList(page.getRecords(),PenVO.class));
        penVOPage.setTotal(page.getTotal());
        penVOPage.setCurrent(page.getCurrent());
        penVOPage.setSize(page.getSize());
        return penVOPage;
    }


}
